package com.plume.toolbox.plus;

import android.content.Context;
import android.content.SharedPreferences;
import com.google.gson.Gson;
import com.google.gson.internal.LinkedTreeMap;
import com.hnphqs.MapConverter;
import java.util.HashMap;

public class UserInfoSaver {

    private static final String PREF_NAME = "AppPreferences";
    private static final String RESPONSE_DATA_KEY = "userInfo";

    public static void saveResponse(Context context, String responseData) {
        SharedPreferences sharedPreferences = context.getSharedPreferences(PREF_NAME, Context.MODE_PRIVATE);
        SharedPreferences.Editor editor = sharedPreferences.edit();
        editor.putString(RESPONSE_DATA_KEY, responseData);
        editor.apply(); // 或者使用 editor.commit(); 但 apply() 是异步提交
        if(!(responseData==null)){
        HashMap data=new Gson().fromJson(responseData,HashMap.class);
        
        HashMap user_info=MapConverter.toHashMap((LinkedTreeMap)data.get("user_info"));
        HashMap auth=MapConverter.toHashMap((LinkedTreeMap)data.get("auth"));
        
        saveUserName(context,(String)user_info.get("nickname"));
        saveToken(context,(String)auth.get("token"));
        }else{
            saveUserName(context,null);
            saveToken(context,null);
        }
        
    }

    public static String loadResponse(Context context) {
        SharedPreferences sharedPreferences = context.getSharedPreferences(PREF_NAME, Context.MODE_PRIVATE);
        return sharedPreferences.getString(RESPONSE_DATA_KEY, null); // 返回默认值 null 如果没有找到数据
    }
    
    public static void saveLoginStatus(Context context, boolean isLoggedIn) {
    SharedPreferences sharedPreferences = context.getSharedPreferences(PREF_NAME, Context.MODE_PRIVATE);
    SharedPreferences.Editor editor = sharedPreferences.edit();
    editor.putBoolean("isLogin", isLoggedIn);
    editor.apply(); // 或者使用 editor.commit(); 但 apply() 是异步提交
}
   public static boolean checkLoginStatus(Context context) {
    SharedPreferences sharedPreferences = context.getSharedPreferences(PREF_NAME, Context.MODE_PRIVATE);
    return sharedPreferences.getBoolean("isLogin", false);
}
    public static void saveUserName(Context context,String userName){
        SharedPreferences sharedPreferences = context.getSharedPreferences(PREF_NAME, Context.MODE_PRIVATE);
        SharedPreferences.Editor editor = sharedPreferences.edit();
        editor.putString("user_name", userName);
        editor.apply(); // 或者使用 editor.commit(); 但 apply() 是异步提交
    }
    
    public static String loadUserName(Context context) {
        SharedPreferences sharedPreferences = context.getSharedPreferences(PREF_NAME, Context.MODE_PRIVATE);
        return sharedPreferences.getString("user_name", null); // 返回默认值 null 如果没有找到数据
    }
    
    public static void saveToken(Context context,String userName){
        SharedPreferences sharedPreferences = context.getSharedPreferences(PREF_NAME, Context.MODE_PRIVATE);
        SharedPreferences.Editor editor = sharedPreferences.edit();
        editor.putString("token", userName);
        editor.apply(); // 或者使用 editor.commit(); 但 apply() 是异步提交
    }
    
    public static String loadToken(Context context) {
        SharedPreferences sharedPreferences = context.getSharedPreferences(PREF_NAME, Context.MODE_PRIVATE);
        return sharedPreferences.getString("token", null); // 返回默认值 null 如果没有找到数据
    }
    
}
